package exttable.userdefineextendsclasses.userDefineJFileChooser;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UserDefineJFileChooserSelectDirOnly extends JPanel{
    private JTextField saveFilesToJTextField;
	private JButton openFileJButton;
    private JFileChooser jFileChooser;
    private int retVal;

    public JTextField getSaveFilesToJTextField() {
        return saveFilesToJTextField;
    }

    public void setSaveFilesToJTextField(JTextField saveFilesToJTextField) {
        this.saveFilesToJTextField = saveFilesToJTextField;
    }

    public UserDefineJFileChooserSelectDirOnly(){
        jFileChooser=new JFileChooser();
        jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        saveFilesToJTextField=new JTextField();
        saveFilesToJTextField.setPreferredSize(new Dimension(300,20));
        saveFilesToJTextField.setEnabled(false);
        openFileJButton=new JButton("browse");
        openFileJButton.setPreferredSize(new Dimension(100,20));
        openFileJButton.addActionListener(selectFile());
        add(saveFilesToJTextField);
        add(openFileJButton);
    }

    private ActionListener selectFile(){
        return new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                retVal=jFileChooser.showOpenDialog(openFileJButton);
                if(retVal==JFileChooser.APPROVE_OPTION){
                	saveFilesToJTextField.setText(jFileChooser.getSelectedFile().getAbsolutePath());
                }

            }
        };
    }



}
